﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Excell_On_Services.Models;
using Excell_On_Services.ViewModels;
using Excell_On_Services.Infrastructure.Config;

namespace Excell_On_Services.Controllers
{
    public class DepartmentController : Controller
    {
        public IEOSRepository Repository { get; set; }

        public DepartmentController()
            : this(new EFEOSRepository())
        {

        }

        public DepartmentController(IEOSRepository repository)
        {
            Repository = repository;
        }


        //
        // GET: /Department/
        [Authorize(Roles="Administrators")]
        public ActionResult Index([DefaultValue(0)] int pageIndex)
        {
            //int departmentsPerPage = Configuration.Instance.PostPerPage;
            int departmentsPerPage = 4;
            return ShowDepartments(
                () => Repository.FindDepartmentsInPage(pageIndex, departmentsPerPage),
                () => Repository.GetDepartmentsCount(),
                pageIndex, "Index", departmentsPerPage
                );
            
        }
        [Authorize(Roles = "Administrators")]
        private ActionResult ShowDepartments(Func<IEnumerable<Department>> filterFunc, Func<int> countFunc, int pageIndex, string actionName, int departmentPerPage)
        {
            var departments = filterFunc();
            var departmentCount = countFunc();
            var model = new DepartmentsDisplayModel
            {
                Departments = departments,
                PagerModel = new PagerModel
                {
                    CurrentPageIndex = pageIndex,
                    ItemsPerPage = departmentPerPage,
                    TotalItems = departmentCount,
                    PageUrlTemplate = Url.Action(actionName, new { pageIndex = PagerModel.PageSymbol })

                }
            };
            return View("Index", model);

        }

        //
        // GET: /Department/Details/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Details(int id)
        {
            var department = Repository.FindDeparmentById(id);
            var model = new DepartmentModel
            {
                DepartmentID = id,
                DepartmentName = department.DepartmentName,
                Description = department.Description
            };
            return View(model);
        }

        //
        // GET: /Department/Create
        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Department/Create

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Create(DepartmentModel model)
        {
            try
            {
                // TODO: Add insert logic here
                if (!ModelState.IsValid)
                    return View(model);
                var department = new Department
                {
                    DepartmentName = model.DepartmentName,
                    Description = model.Description,
                };
                Repository.AddDepartment(department);
                Repository.CommitChanges();
                return RedirectToAction("Details", new { id = department.DepartmentID});
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Department/Edit/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id)
        {
            var deparment = Repository.FindDeparmentById(id);
            var model = new DepartmentModel
            {
                DepartmentID = deparment.DepartmentID,
                DepartmentName = deparment.DepartmentName,
                Description = deparment.Description
            };
            return View(model);
        }

        //
        // POST: /Department/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id, DepartmentModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                    return View(model);
                var item = Repository.FindDeparmentById(model.DepartmentID.Value);
                item.DepartmentName = model.DepartmentName;
                item.Description = model.Description;
                Repository.CommitChanges();
                return RedirectToAction("Details", new {id = item.DepartmentID });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Department/Delete/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id)
        {
            var department = Repository.FindDeparmentById(id);
            return View(department);
        }

        //
        // POST: /Department/Delete/5

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id, Department model)
        {
            try
            {
                //return Content("<script language='javascript' type='text/javascript'>alert('Department cannot delete because there are have employees');</script>",);
                 //TODO: Add delete logic here
                if (Repository.DeleteDepartment(id))
                {
                    Repository.CommitChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewData["message"] = "Department cannot delete because there are have employees";
                    return RedirectToAction("Details", new { id = id});
                }
                
            }
            catch
            {
                var department = Repository.FindDeparmentById(id);
                return View();
            }
        }
    }
}
